package com.atguigu.kafka.produce.demo;

import org.apache.kafka.clients.producer.Partitioner;
import org.apache.kafka.common.Cluster;
import org.apache.kafka.common.PartitionInfo;

import java.util.List;
import java.util.Map;

/**
 * @ClassName MyPartitioner
 * @DeScription MyPartitioner
 * @Author 剧情再美终是戏
 * @Date 2019/12/4 18:29
 * @Version 1.0
 **/
public class MyPartitioner implements Partitioner {

    public int partition(String topic, Object key, byte[] keyBytes, Object value, byte[] valueBytes, Cluster cluster) {
        // 获取分区数据
        List<PartitionInfo> partitions = cluster.partitionsForTopic(topic);
        int numPartitions = partitions.size();
        int keyInt = Integer.parseInt(key.toString());
        if (0 == keyInt) {
            return 0;
        } else {
            return 1;
        }
    }

    public void close() {

    }

    public void configure(Map<String, ?> configs) {

    }
}
